package com.lcy.mapstruct;

import com.lcy.mapstruct.helper.MapStructHelper;
import com.lcy.model.dto.postComment.PostCommentRequest;
import com.lcy.model.entity.OjPostComment;
import com.lcy.model.vo.PostCommentVo;
import org.mapstruct.*;

import java.util.List;

/**
 * 帖子评论MapStruct转换器
 * @author zhang.rx
 * @since 2024/5/20
 */
@Mapper(
    componentModel = "spring", 
    unmappedTargetPolicy = ReportingPolicy.IGNORE,
    uses = {MapStructHelper.class}
)
public interface OjPostCommentConverter {

    /**
     * 将请求DTO转换为实体对象
     */
    OjPostComment toEntity(PostCommentRequest req);

    /**
     * 将实体对象转换为VO对象
     * 确保所有字段正确映射
     */
    @Mapping(target = "expandedFlag", constant = "false")
    @Mapping(target = "children", ignore = true)
    @Mapping(target = "content", source = "content", qualifiedByName = "contentToString")
    PostCommentVo toVo(OjPostComment comment);

    /**
     * 将实体对象列表转换为VO对象列表
     */
    List<PostCommentVo> toVoList(List<OjPostComment> list);
    
    /**
     * 内容对象转字符串处理
     */
    @Named("contentToString")
    default String contentToString(Object content) {
        if (content == null) {
            return "";
        }
        try {
            // 直接将内容转换为字符串
            return content.toString();
        } catch (Exception e) {
            return "";
        }
    }
}
